import groovy.xml.MarkupBuilder

/**
 * I only half understand this, copy/paste from existing examples...
 * Need help to sync raw YUI with Grails-approach
 * Currently focusing on "yui" controller and view(s) 
 */
class TreeController {

    def index = { redirect(action:list,params:params) }

      // the delete, save and update actions only accept POST requests
      static allowedMethods = [delete:'POST', save:'POST', update:'POST']

      def list = {
          def writer = new StringWriter()
          def xml = new MarkupBuilder(writer)
          xml.corpus(name: "Corpus Tree"){
              Corpus.list().each {
                  Corpus corp = it
                  corpus(name:corp.name, id:corp.id) {
                      corp.books.each {
                          book(name:it.title, id:it.id)

                      }
                  }
              }
          }

          if(!params.max)params.max = 10
//          [ tagInstanceList: Tag.list( params ), tagInstanceTotal: Tag.count() ]
           [ "data": writer.toString(), corpusInstanceList: Corpus.list( params ), corpusInstanceTotal: Corpus.count()]

      }

//      def show = {
//          def treeInstance = Tree.get( params.id )
//
//          if(!treeInstance) {
//              flash.message = "Tree not found with id ${params.id}"
//              redirect(action:list)
//          }
//          else { return [ treeInstance : treeInstance ] }
//      }


//      def delete = {
//          def treeInstance = Tree.get( params.id )
//          if(treeInstance) {
//              try {
//                  treeInstance.delete()
//                  flash.message = "Tree ${params.id} deleted"
//                  redirect(action:list)
//              }
//              catch(org.springframework.dao.DataIntegrityViolationException e) {
//                  flash.message = "Tree ${params.id} could not be deleted"
//                  redirect(action:show,id:params.id)
//              }
//          }
//          else {
//              flash.message = "Tree not found with id ${params.id}"
//              redirect(action:list)
//          }
//      }

//      def edit = {
//          def treeInstance = Tree.get( params.id )
//
//          if(!treeInstance) {
//              flash.message = "Tree not found with id ${params.id}"
//              redirect(action:list)
//          }
//          else {
//              return [ treeInstance : treeInstance ]
//          }
//      }

//      def update = {
//          def treeInstance = Tree.get( params.id )
//          if(treeInstance) {
//              if(params.version) {
//                  def version = params.version.toLong()
//                  if(treeInstance.version > version) {
//
//                      treeInstance.errors.rejectValue("version", "tree.optimistic.locking.failure", "Another user has updated this Tree while you were editing.")
//                      render(view:'edit',model:[treeInstance:treeInstance])
//                      return
//                  }
//              }
//              treeInstance.properties = params
//              if(!treeInstance.hasErrors() && treeInstance.save()) {
//                  flash.message = "Tree ${params.id} updated"
//                  redirect(action:show,id:treeInstance.id)
//              }
//              else {
//                  render(view:'edit',model:[treeInstance:treeInstance])
//              }
//          }
//          else {
//              flash.message = "Tree not found with id ${params.id}"
//              redirect(action:edit,id:params.id)
//          }
//      }

//      def create = {
//          def treeInstance = new Tree()
//          treeInstance.properties = params
//          return ['treeInstance':treeInstance]
//      }
//
//      def save = {
//          def treeInstance = new Tree(params)
//          if(!treeInstance.hasErrors() && treeInstance.save()) {
//              flash.message = "Tree ${treeInstance.id} created"
//              redirect(action:show,id:treeInstance.id)
//          }
//          else {
//              render(view:'create',model:[treeInstance:treeInstance])
//          }
//      }
}
